﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

using System.IO;
using System.Diagnostics; 

namespace prjEMR22_154
{
    public partial class mainFrom : Form
    {

        public mainFrom()
        {
            InitializeComponent();
        }

        private void mainFrom_Load(object sender, EventArgs e)
        {
            if (login.strUType == "管理员")
            {
                label1.Text = "欢迎管理员！";
            }
            else
            {
                label1.Text = "欢迎用户！";
            }
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {

        }

        private void toolStripMenuItem1_Click(object sender, EventArgs e)
        {

        }

        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            string choose = treeView1.SelectedNode.Text;
            switch (choose)
            {
                case "数据备份":
                    data_save();
                    break;
                case "药物管理":
                    {
                        if (login.strUType == "管理员")
                        {
                            Medicine PatientInfo = new Medicine(); //实例化一个子窗口
                            //设置子窗口不显示为顶级窗口
                            PatientInfo.TopLevel = false;
                            //设置子窗口的样式，没有上面的标题栏
                            PatientInfo.FormBorderStyle = FormBorderStyle.None;
                            //填充
                            PatientInfo.Dock = DockStyle.Fill;
                            //清空Panel里面的控件 
                            this.contentPanel.Controls.Clear();
                            //加入控件
                            this.contentPanel.Controls.Add(PatientInfo);
                            //让窗体显示
                            PatientInfo.Show();
                        }
                        else
                        {
                            MessageBox.Show("权限不足");
                        }
                    }
                    break;
                case "查询编辑病人":
                    {
                        PatientInfo_inquire PatientInfo = new PatientInfo_inquire(); //实例化一个子窗口
                        //设置子窗口不显示为顶级窗口
                        PatientInfo.TopLevel = false;
                        //设置子窗口的样式，没有上面的标题栏
                        PatientInfo.FormBorderStyle = FormBorderStyle.None;
                        //填充
                        PatientInfo.Dock = DockStyle.Fill;
                        //清空Panel里面的控件 
                        this.contentPanel.Controls.Clear();
                        //加入控件
                        this.contentPanel.Controls.Add(PatientInfo);
                        //让窗体显示
                        PatientInfo.Show();
                    }
                    break;
                case "添加病人":
                    {
                        PatientInfo_new PatientInfo = new PatientInfo_new(); //实例化一个子窗口
                        //设置子窗口不显示为顶级窗口
                        PatientInfo.TopLevel = false;
                        //设置子窗口的样式，没有上面的标题栏
                        PatientInfo.FormBorderStyle = FormBorderStyle.None;
                        //填充
                        PatientInfo.Dock = DockStyle.Fill;
                        //清空Panel里面的控件 
                        this.contentPanel.Controls.Clear();
                        //加入控件
                        this.contentPanel.Controls.Add(PatientInfo);
                        //让窗体显示
                        PatientInfo.Show();
                    }
                    break;
                case "项目管理":
                    {
                        Inspect PatientInfo = new Inspect(); //实例化一个子窗口
                        //设置子窗口不显示为顶级窗口
                        PatientInfo.TopLevel = false;
                        //设置子窗口的样式，没有上面的标题栏
                        PatientInfo.FormBorderStyle = FormBorderStyle.None;
                        //填充
                        PatientInfo.Dock = DockStyle.Fill;
                        //清空Panel里面的控件 
                        this.contentPanel.Controls.Clear();
                        //加入控件
                        this.contentPanel.Controls.Add(PatientInfo);
                        //让窗体显示
                        PatientInfo.Show();
                    }
                    break;

                default: break;
            }

        }

        private void data_save()
        {
            // MySQL连接信息  
            string server = "localhost";
            string uid = "root";
            string pwd = "123456";
            string database = "emr"; // 根据你的实际数据库名称修改  
            string charset = "utf8";

            // mysqldump命令模板  
            string backupsql = string.Format("mysqldump --host={0} --default-character-set={1} --lock-tables --routines --force --port=3306 --user={2} --password={3} --quick ",
                server, charset, uid, pwd);

            // mysqldump工具路径  
            string mysqldumpPath = @"E:\mysql-5.7.35-winx64\mysql-5.7.35-winx64\bin";

            // 创建保存文件对话框  
            using (SaveFileDialog sfd = new SaveFileDialog())
            {
                sfd.Filter = "SQL Files (*.sql)|*.sql"; // 设置文件类型过滤器  
                sfd.DefaultExt = "sql"; // 设置默认扩展名  
                sfd.Title = "选择备份文件位置并命名";
                sfd.AddExtension = true; // 自动添加扩展名如果用户未输入  

                if (sfd.ShowDialog() == DialogResult.OK)
                {
                    // 用户选择的备份文件路径  
                    string filePath = sfd.FileName;

                    // 构建mysqldump命令  
                    string cmd = backupsql + database + " > \"" + filePath + "\"";

                    // 执行命令  
                    string result = RunCmd(mysqldumpPath, cmd);

                    // 显示结果（这里可以改为更友好的UI提示）  
                    if (string.IsNullOrEmpty(result) || result.Contains("Warning"))
                    {
                        MessageBox.Show("数据库备份成功！", "提示");
                    }
                    else
                    {
                        MessageBox.Show("数据库备份失败：" + result, "错误");
                    }
                }
            }
            
        }

        /// <summary>  
        /// 执行命令行命令的函数  
        /// </summary>  
        /// <param name="workingDirectory">工作目录</param>  
        /// <param name="cmd">命令行命令</param>  
        /// <returns>命令执行的错误信息（如果有的话）</returns>  
        private string RunCmd(string workingDirectory, string cmd)
        {
            Process p = new Process();
            p.StartInfo.FileName = "cmd.exe";
            p.StartInfo.WorkingDirectory = workingDirectory;
            p.StartInfo.UseShellExecute = false;
            p.StartInfo.RedirectStandardInput = true;
            p.StartInfo.RedirectStandardOutput = true;
            p.StartInfo.RedirectStandardError = true;
            p.StartInfo.CreateNoWindow = true;
            p.Start();
            p.StandardInput.WriteLine(cmd);
            p.StandardInput.WriteLine("exit");
            p.StandardInput.Close();
            p.WaitForExit();
            return p.StandardError.ReadToEnd();
        }

        private void menuPanel_Paint(object sender, PaintEventArgs e)
        {

        }

        private void contentPenal_Paint(object sender, PaintEventArgs e)
        {

        }

        private void tableLayoutPanel1_Paint(object sender, PaintEventArgs e)
        {

        }

        private void saveFileDialog1_FileOk(object sender, CancelEventArgs e)
        {

        }

        private void toolStripMenuItem1_Click_1(object sender, EventArgs e)
        {

        }

        private void fontDialog1_Apply(object sender, EventArgs e)
        {

        }
    }
    
}
